rm(list=ls())

library(haven)
library(tidyverse)
library(scales)
library(fastDummies)
library(sjmisc)

## Data source: https://search.gesis.org/research_data/ZA7863
## Note that this figure requires data that we cannot make available publicly

dat <- haven::read_dta('data_private/ZA7863_v1-0-0.dta') %>%
  filter(WELLE %in% c('202105s1'))

## Pivot longer and clean up labels 

dat <- dat %>%
  dplyr::select(one_of('PAALÖSO', 'PAALTER', 'PAASUFPB', 
                       'PAAUSSEN', 'PADIGITA', 'PADTGDCK', 
                       'PAFAMILI', 'PAUMWELT',  
                       'PAWISTA')) %>%
  pivot_longer(cols = 1:ncol(.),
               names_to = 'issue',
               values_to = 'party') %>%
  filter(!is.na(party)) %>%
  mutate(issue_recoded = recode(issue,
                                `PAAUSSEN` = 'foreign \naffairs',
                                `PAALÖSO` = 'wages',
                                `PAASUFPB` = 'immigration',
                                `PAALTER` = 'pensions',
                                `PADIGITA` = 'digitization',
                                `PADTGDCK` = 'covid 19',
                                `PAFAMILI` = 'family\npolicy',
                                `PAUMWELT` = 'climate change/\nenvironment',
                                `PAWISTA` = 'economics'),
         party_recoded = sjmisc::to_label(party),
         issue_recoded = str_to_title(issue_recoded)) %>%
  mutate(party_recoded = as.character(party_recoded),
         party_recoded = ifelse(party %in% c(1:5, 33), party_recoded, 'other'))

## Aggregate by party and issue 

plot_df <- fastDummies::dummy_cols(dat, select_columns = 'party_recoded',
                                   remove_selected_columns = T) %>%
  group_by(issue_recoded) %>%
  summarise_at(vars(contains('party_recoded_')), mean, na.rm = T) %>%
  pivot_longer(cols = 2:ncol(.),
               names_to = 'party',
               values_to = 'share') %>%
  mutate(issue_recoded = fct_relevel(issue_recoded, 'Climate Change/\nEnvironment'),
         party = str_remove_all(party, 'party_recoded_')) %>%
  mutate(party = recode(party, 
                        `Alternative für Deutschland, AfD` = 'AfD',
                        `Bündnis 90/Grüne` = 'Greens',
                        `Die Linke` = 'Left')) %>%
  mutate(party = fct_relevel(party, 'other',  'Left', 'AfD', 'FDP', 'SPD','CDU/CSU','Greens'))

## Arrange issue levels by Green party ownership

issue_levels <- plot_df %>% filter(party == 'Greens') %>% arrange(share) %>% pull(issue_recoded) %>% as.character()

plot_df <- plot_df %>% 
  mutate(issue_recoded = fct_relevel(issue_recoded, issue_levels))

## Plot this 

p1 <- ggplot(plot_df %>% filter(party != 'other'), aes(x = issue_recoded, 
                                                       group = party, 
                                                       fill = party,
                                                       y  = share)) + 
  geom_bar(stat = 'identity', width = .5, col = 'black') +
  scale_fill_manual(values = c('darkviolet', 'blue', 'yellow3', 'red','black','forestgreen')) + 
  theme_bw() + 
  scale_y_continuous(labels = scales::percent, 
                     breaks = seq(0, 1.01, .2),
                     limits = c(0, 1.1)) + 
  labs(y = 'Party perceived as most competent\n (share of respondents, by issue area)',
       x = '',
       fill = '') + 
  coord_flip() 
p1

